﻿using NetFrame.Model;
using NetFrame.Persistance;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetFrame.UI
{
    public partial class JobManagement : Form
    {

        public List<Job> _jobs=new List<Job>();
        
        public JobManagement()
        {

            InitializeComponent();
            InitiaData();
            jobDataView.DataSource = _jobs;
        }

        private void InitiaData()
        {
            BasePersistance<Job>.DoAction((t) =>
            {
                _jobs = t.Adapter.Select().OrderBy(k => k.Id).ToList();
            });
            
        }

        private void jobDataView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var job = ((List<Job>)((DataGridView)sender).DataSource)[e.RowIndex];
            var edit = new JobEdit(job);
            edit.ShowDialog();
            InitiaData();
            jobDataView.DataSource = _jobs;
            
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            var job = new Job();
            job.RowStatus = 1;
            job.RegCron = "1 * * * * ?";
            job.JobName = "python_";
            var edit = new JobEdit(job);
            edit.ShowDialog();
            InitiaData();
            jobDataView.DataSource = _jobs;
        }

        private void delButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除？", "请确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                if (jobDataView.SelectedRows.Count <= 0) return;
                BasePersistance<Job>.DoAction((t) =>
                {
                    t.Adapter.Delete(_jobs[jobDataView.SelectedRows[0].Index]);
                });
                InitiaData();
                jobDataView.DataSource = _jobs;
            }
        }
    }
}
